package com.sec.etech.ism.service;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.sec.etech.ism.model.IsmAppSetting;
import com.sec.etech.ism.util.SapSignatureUtil;
import org.openbpm.base.core.util.StringUtil;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class IsmSyncPartsSapNumService {
    @Resource
    JdbcTemplate jdbcTemplate;
    @Resource
    IsmCommonService ismCommonService;
    @Resource
    IsmAppSettingService ismAppSettingService;

    public void syncPartsSapNum() throws Exception{

        String sql = "select t2.partsid,t2.parts_SAPnum from temp_parts_sapnum t2 where t2.flag=0";
        List<Map<String,Object>> partsList = jdbcTemplate.queryForList(sql);
        if(partsList==null || partsList.size()==0){
            return;
        }
        IsmAppSetting appSetting = ismAppSettingService.getByCode("sap");
        String url = ismCommonService.getSapPreUrl(appSetting.getAppAddress(), appSetting.getAppConfig()) + "/ZPP_BOM_SINGLE_EXP_ALL";

        for(Map<String,Object> map:partsList){
            try {
                String partsSAPnum = map.get("parts_SAPnum").toString();
                System.out.println("syncPartsSapNum --> parts_SAPnum = " + partsSAPnum);
                String materialSAPnum = null;
                String materialNum = null;
                String billetSAPnum = null;
                String billetNum = null;
                //查询毛坯物料号
                List<JSONObject> oStpoxList = this.getSapBom(partsSAPnum, url, appSetting.getAppKey2());
                if (oStpoxList != null && oStpoxList.size() == 1) {
                    String idnrk = oStpoxList.get(0).getString("IDNRK");
                    //if (StringUtil.isNotEmpty(idnrk) && idnrk.startsWith(partsSAPnum.substring(0, idnrk.length() - 1))) {
                    if (StringUtil.isNotEmpty(idnrk)) {
                        billetSAPnum = idnrk;
                        billetNum = "1";
                        //查询原材料物料号
                        List<JSONObject> oStpoxList2 = this.getSapBom(billetSAPnum, url, appSetting.getAppKey2());
                        if (oStpoxList2 != null && oStpoxList2.size() == 1 ) {
                            materialSAPnum = oStpoxList2.get(0).getString("IDNRK");
                            materialNum = oStpoxList2.get(0).getString("MENGE");
                        }
                    }
                }
                sql = "update temp_parts_sapnum set flag=1, billet_SAPnum=?, billet_num=?, material_SAPnum=?, material_num=? where partsid=?";
                jdbcTemplate.update(sql, billetSAPnum, billetNum, materialSAPnum, materialNum, map.get("partsid").toString());
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    private List<JSONObject> getSapBom(String sAPnum, String url, String appKey) throws Exception{
        JSONObject sapParams = new JSONObject();
        sapParams.put("I_WERKS", "1303");
        sapParams.put("I_MATNR", sAPnum);
        sapParams.put("I_DATE", "20221231");
        sapParams.put("I_STLAN", "1");
        sapParams.put("I_MENGE", "1");
        sapParams.put("I_S_RAD3", "X");
        sapParams.put("I_LEVEL_NUM", "1");

        JSONObject bodyObj = SapSignatureUtil.buildRequestParams(sapParams, appKey);
        String body = JSONObject.toJSONString(bodyObj);
        String result = HttpUtil.post(url, body, 2*60*1000);
        JSONObject resultObj = JSONObject.parseObject(result);
        JSONObject data = resultObj.getJSONObject("data");
        //System.out.println("syncPartsSapNum --> O_STPOX = " + data.getString("O_STPOX"));
        List<JSONObject> oStpoxList = data.get("O_STPOX")!=null ? (List<JSONObject>) data.get("O_STPOX") : null;
        return oStpoxList;
    }
}
